/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.documentar.DAO;

import br.com.documentar.interfacee.InterfaceDAO;
import br.com.documentar.model.SetorModel;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Santos
 */
public class SetorDAO extends AbstractDAO implements InterfaceDAO<List<SetorModel>, SetorModel> {

    @Override
    public boolean setDados(SetorModel model) {
        String sql = null;
        ConnectDataBase();
        switch (model.getActionScript()) {
            case "I":
                sql = "INSERT INTO \n"
                        + "  `tset_setor`\n"
                        + "(\n"
                        //+ "  `TSET_CODIGO_PK`,\n"//auto incremento
                        + "  `TSET_NOME`) \n"
                        + "VALUE (\n"
                        //+ "  :TSET_CODIGO_PK,\n"
                        + "  '" + model.getNome() + "')";
                break;
            case "U":
                sql = "UPDATE `tset_setor`  \n"
                        + "SET `TSET_NOME` = '" + model.getNome() + "' \n"
                        + "WHERE `TSET_CODIGO_PK` = '" + model.getCodigo() + "'";
                break;
            case "D":
                sql = "DELETE FROM `tset_setor` \n"
                        + "WHERE `TSET_CODIGO_PK` = '" + model.getCodigo() + "'";
                break;
        }
        return InsertUpdateDelete(sql, model.getNomeTela());
    }

    @Override
    public List<SetorModel> getDados(SetorModel model) {
        model.setResultSet(null);
        model.getListaSetor().clear();
        String sql = "SELECT * FROM `tset_setor`";
        try {
            ConnectDataBase();
            switch (model.getActionScript()) {
                case "C":
                    model.setResultSet(Select(sql, model.getNomeTela()));
                    break;
                case "P":
                    model.setResultSet(Select(sql + " WHERE TSET_NOME LIKE '%" + model.getNome() + "%'", model.getNomeTela()));
                    break;
            }
            while (model.getResultSet().next()) {
                SetorModel p = new SetorModel();
                p.setCodigo(rs.getInt("TSET_CODIGO_PK"));
                p.setNome(rs.getString("TSET_NOME"));
                model.getListaSetor().add(p);
            }
        } catch (SQLException ex) {
            ex.getStackTrace();
        } finally {
            Desconectar(model.getNomeTela());
        }
        return model.getListaSetor();
    }
}
